﻿Public Class FigureManager
    Public Sub Init()
        m_FigureList = New ArrayList
        m_SelectedList = New ArrayList

        AddHandler main.MouseDownEvent, AddressOf Me.mousedown
        AddHandler main.MouseUpEvent, AddressOf Me.mouseup
        AddHandler main.MouseMoveEvent, AddressOf Me.mousemove
        AddHandler main.KeyDownEvent, AddressOf Me.keydown
        AddHandler main.KeyUpEvent, AddressOf Me.keyup

        m_FunctionType = 0
    End Sub

    '添加图形
    Public Sub Add(figure As FigureElement)
        m_FigureList.Add(figure)
    End Sub

    '删除图形
    Public Sub Del(figure As FigureElement)
        m_FigureList.Remove(figure)
    End Sub

    '选中图形
    Public Sub SelectFigure(ByRef figure As FigureElement)
        m_SelectedList.Add(figure)
        figure.SetSelected(True)
    End Sub

    '取消选中图形
    Public Sub UnSelectFigure(ByRef figure As FigureElement)
        m_SelectedList.Remove(figure)
        figure.SetSelected(False)
    End Sub

    '全部取消
    Public Sub UnSelectAll()
        Dim fg As FigureElement
        For i = 0 To m_SelectedList.Count - 1
            fg = m_SelectedList.Item(i)
            fg.SetSelected(False)
        Next
        m_SelectedList.Clear()
    End Sub

    '全选
    Public Sub SelectAll()

    End Sub

    Public Sub SetFunctionType(type As Integer)
        m_FunctionType = type
    End Sub

    Public Function GetFunctionType() As Integer
        Return m_FunctionType
    End Function

    '鼠标左键按下
    Protected Overridable Sub mousedown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs)
        m_oldpt = e.Location
        Select Case m_FunctionType

            '选择
            Case 0
                Dim flag As Boolean = False

                Dim i As Integer = m_FigureList.Count
                Dim sel As FigureElement
                While i <> 0
                    sel = m_FigureList.Item(i - 1)
                    If (sel.GetRect.Contains(e.Location)) Then

                        If m_Key(0) = False Then        '单选
                            flag = True
                            m_FunctionFlag(0) = True
                            If m_SelectedList.Contains(sel) Then
                                Exit While
                            End If

                            UnSelectAll()
                            SelectFigure(sel)

                            Exit While

                        Else                            '多选

                            m_FunctionFlag(0) = True
                            If sel.IsSelected Then
                                UnSelectFigure(sel)
                            Else
                                SelectFigure(sel)
                            End If
                        End If
                        
                    End If

                    i = i - 1
                End While

                If m_Key(0) = False Then
                    If flag = False Then
                        UnSelectAll()
                    End If
                End If

                Exit Select

            Case 1


        End Select
    End Sub


    '鼠标移动
    Protected Overridable Sub mousemove(sender As System.Object, e As System.Windows.Forms.MouseEventArgs)
        Trace.WriteLine(m_SelectedList.Count)
        Select Case m_FunctionType

            '选择
            Case 0
                Dim fg As FigureElement
                If m_FunctionFlag(0) = True Then        '移动
                    For i = 0 To m_SelectedList.Count - 1
                        fg = m_SelectedList.Item(i)
                        fg.Move(fg.GetRect.X - (m_oldpt.X - e.X), fg.GetRect.Y - (m_oldpt.Y - e.Y))
                        fg.BufferRender()
                    Next

                    m_Parent.RenderEx()
                    m_oldpt = e.Location
                End If
                Exit Select


            Case 1


        End Select
    End Sub


    '鼠标左键弹起
    Protected Overridable Sub mouseup(sender As System.Object, e As System.Windows.Forms.MouseEventArgs)
        For i = 0 To m_FunctionFlag.Length - 1
            m_FunctionFlag(i) = False
        Next

        Select Case m_FunctionType

            '选择
            Case 0


                Exit Select
            Case 1


        End Select
    End Sub

    '键盘按下
    Protected Overloads Sub keydown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs)
        Select Case e.KeyCode
            Case Keys.ShiftKey

                m_Key(0) = True
                Exit Select
            Case Keys.ControlKey
        End Select

    End Sub

    '键盘弹起
    Protected Overloads Sub keyup(sender As System.Object, e As System.Windows.Forms.KeyEventArgs)
        Select Case e.KeyCode
            Case Keys.ShiftKey

                m_Key(0) = False
                Exit Select
            Case Keys.ControlKey
        End Select
    End Sub

    Public Sub SetParent(ByRef canvas As Canvas)
        m_Parent = canvas
    End Sub

    Private m_Parent As Canvas

    Public m_FigureList As ArrayList            '所有图形列表
    Public m_SelectedList As ArrayList          '选中图形列表
    Private m_FunctionType As Integer

    '0：移动 1：缩放
    Private m_FunctionFlag(32) As Boolean
    Private m_oldpt As Point

    '按键列表
    '0：SHIFT 1:CTRL
    Private m_Key(32) As Boolean

End Class
